package com.zuijin.vue2blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zuijin.vue2blog.common.utils.JwtUtil;
import com.zuijin.vue2blog.dto.UserComDetails;
import com.zuijin.vue2blog.dto.UserComParam;
import com.zuijin.vue2blog.entity.Role;
import com.zuijin.vue2blog.entity.UserCom;
import com.zuijin.vue2blog.mapper.UserComMapper;
import com.zuijin.vue2blog.service.UserComDetailsService;
import com.zuijin.vue2blog.service.UserComService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 普通用户表 服务实现类
 * </p>
 *
 * @author 醉瑾
 * @since 2022-04-22
 */
@Service
public class UserComServiceImpl extends ServiceImpl<UserComMapper, UserCom> implements UserComService {
    @Resource
    private UserComDetailsService userComDetailsService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public UserCom register(UserComParam userComParam) {
        UserCom userCom = new UserCom();
        BeanUtils.copyProperties(userComParam, userCom);
        userCom.setCreateDate(new Date());
        userCom.setState(0);

        // 查询是否有相同用户名
        QueryWrapper<UserCom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UserCom::getUsername, userCom.getUsername());

        if (baseMapper.exists(queryWrapper)) {
            return null;
        }

        // 加密密码
        String encodePassword = passwordEncoder.encode(userCom.getPassword());
        userCom.setPassword(encodePassword);
        int insert = baseMapper.insert(userCom);
        if (insert == 0) {
            return null;
        }
        return userCom;
    }

    @Override
    public String login(UserComParam userComParam) {
        String token = null;
        // 客户端加密密码后传递
        try {
            UserDetails userDetails = userComDetailsService.loadUserByUsername(userComParam.getUsername());

            if (!passwordEncoder.matches(userComParam.getPassword(), userDetails.getPassword())) {
                System.out.println("密码不正确");
                return null;
            }
            if (!userDetails.isEnabled()) {
                System.out.println("账号已被禁用");
                return null;
            }
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, null);
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            token = jwtUtil.generateToken(userDetails);
        } catch (AuthenticationException e) {
            System.out.println("登录异常");
        }
        return token;
    }
}
